package com.lxd.redis;

import com.lxd.entity.TokenUserInfoDto;
import com.lxd.entity.po.User;
import com.lxd.entity.vo.ActivityVO;
import com.lxd.entity.vo.ClubVO;
import com.lxd.mappers.CommentMapper;
import jdk.nashorn.internal.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author Dongdong
 * @注释
 */
@Component
public class RedisComponent {
    @Autowired
    private RedisUtil redis;

    /**
     * 获取token对应的用户信息
     *
     * @param token
     * @return
     */
    public TokenUserInfoDto getTokenUserInfoDto(String token) {
        TokenUserInfoDto tokenUserInfoDto = (TokenUserInfoDto) redis.get(token);
        return tokenUserInfoDto;
    }

    final static String LIKE_PREFIX = "like-";
    final static String LOCK_SUFFIX = "-lock";
    final static String AAA = "-";

    /**
     * 一个用户对一个评论 点赞操作
     *
     * @param userId
     * @param commentId
     */
    public String setLikeCount(String userId, String commentId) {
        String likeKey = LIKE_PREFIX + commentId;
        String lockKey = userId + AAA + commentId + LOCK_SUFFIX;
        String lockValue = "lockvalue"; // 使用唯一标识作为锁的值
        // 获取锁成功，说明该用户未对该评论点赞
        if (redis.setnxLock(lockKey, lockValue)) {
            redis.incr(likeKey);
            return redis.get(likeKey).toString();
        } else {
            // 如果获取锁失败，说明已经点赞过，执行取消点赞操作
            if (redis.releaseLock(lockKey, lockValue)) { // 先释放锁
                redis.decr(likeKey); // 再执行取消点赞操作
                return redis.get(likeKey).toString();
            }
        }

        return "操作失败";
    }

    /**
     * 获取点赞数量
     *
     * @param commentId
     * @return
     */
    public String getLikeCount(String commentId) {
        String likeKey = LIKE_PREFIX + commentId;
        return redis.get(likeKey) == null ? "0" : redis.get(likeKey).toString();
    }

    /**
     * 根据活动id获取活动信息
     *
     * @param id
     * @return
     */
    public ActivityVO getActivityById(Long id) {
        ActivityVO activityVO = (ActivityVO) redis.getHash("ACTIVITY", String.valueOf(id));
        if (activityVO == null) {
            return null;
        }
        return activityVO;
    }

    /**
     * 根据活动获取活动名称
     */
    public String getActivityNameById(Long id) {
        ActivityVO activityVO = (ActivityVO) redis.getHash("ACTIVITY", String.valueOf(id));
        if (activityVO == null) {
            return null;
        }
        return activityVO.getTitle();
    }

    /**
     * 根据社团id获取社团信息
     */
    public ClubVO getClubById(Long clubId) {
        ClubVO club = (ClubVO) redis.getHash("CLUB", String.valueOf(clubId));
        if (club == null) {
            return null;
        }
        return club;
    }

    /**
     * 根据社团id获取社团名称
     *
     * @param clubId
     * @return
     */
    public String getClubNameById(Long clubId) {
        ClubVO club = (ClubVO) redis.getHash("CLUB", String.valueOf(clubId));
        if (club == null) {
            return null;
        }
        return club.getName();
    }

    public String getUserNameById(Long id) {
        User user = (User) redis.getHash("USER", String.valueOf(id));
        if (user == null) {
            return null;
        }
        return user.getUserRealName();
    }
}
